/*
 *  LLTimeManager_ios.cpp
 *
 *
 *  Created by Alexander Trutchenko on 11/02/12.
 *  Copyright 2012 Free Team. All rights reserved.
 *
 */

#include "lid_lib/src/time_managment/LLTimeManager.h"

#include "lid_lib/src/data_managment/LLNotifier.h"

#include "lid_lib/src/config/LLConfig.h"
#include "lid_lib/src/config/LLKeys.h"

#import <Foundation/Foundation.h>
//#import "NSCalendarDate.h"

namespace lidlib {


////////////////////////////////////////////////////////
//
/** LLTimeManager
 *
 */
////////////////////////////////////////////////////////

unsigned int LLTimeManager::getSeconds() 
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:kCFCalendarUnitSecond fromDate:today];
    return (unsigned int)[weekdayComponents second]; 
}

unsigned int LLTimeManager::getMinutes() 
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:kCFCalendarUnitMinute fromDate:today];
    return (unsigned int)[weekdayComponents minute]; 
}

unsigned int LLTimeManager::getHours() 
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:kCFCalendarUnitHour fromDate:today];
    return (unsigned int)[weekdayComponents hour]; 
}

unsigned int LLTimeManager::getMonthDay() 
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:kCFCalendarUnitDay fromDate:today];
    return (unsigned int)[weekdayComponents day]; 
}

unsigned int LLTimeManager::getMonth() 
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:kCFCalendarUnitMonth fromDate:today];
    return (unsigned int)[weekdayComponents month]; 
}


unsigned int LLTimeManager::getYear() 
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:kCFCalendarUnitYear fromDate:today];
    return (unsigned int)[weekdayComponents year]; 
}

ELLWeekDay LLTimeManager::getWeekDay() 
{    
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
    switch ([weekdayComponents weekday] - 1)
	{
        case 0: return ell_wd_sunday;
        case 1: return ell_wd_monday;
        case 2: return ell_wd_tuesday;
        case 3: return ell_wd_wednesday;
        case 4: return ell_wd_thursday;
        case 5: return ell_wd_friday;
        case 6: return ell_wd_saturday;
        default: break;
	}
	return ell_wd_invalid;  
}

////////////////////////////////////////////////////////
// Private interface

void LLTimeManager::update(float dt) 
{
    NSDate *today = [NSDate date];
    
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:(NSYearCalendarUnit |
                                                                 NSMonthCalendarUnit |
                                                                 NSDayCalendarUnit | 
                                                                 NSWeekdayCalendarUnit |
                                                                 NSHourCalendarUnit |
                                                                 NSMinuteCalendarUnit |
                                                                  NSSecondCalendarUnit) fromDate:today]; 
	ELLWeekDay wd = ell_wd_invalid;
	switch ([weekdayComponents weekday] - 1)
	{
        case 0: wd = ell_wd_sunday; break;
        case 1: wd = ell_wd_monday; break;
        case 2: wd = ell_wd_tuesday; break;
        case 3: wd = ell_wd_wednesday; break;
        case 4: wd = ell_wd_thursday; break;
        case 5: wd = ell_wd_friday; break;
        case 6: wd = ell_wd_saturday; break;
        default: break;
	}

    compareTime((unsigned int)[weekdayComponents second], 
                (unsigned int)[weekdayComponents minute], 
                (unsigned int)[weekdayComponents hour], 
                (unsigned int)[weekdayComponents day],
                (unsigned int)[weekdayComponents month],
                (unsigned int)[weekdayComponents year],
                wd);
}

} // namespace lidlib
